import request from '@/utils/request.ts'
import type { Article, ArticleResponseData } from './type'

enum API {
  // 获取全部文章数据
  ARTICLE_GETALL_URL = '/article/admin/getall/',
  // 添加文章
  ADD_ARTICLE_URL = '/article/admin/create/',
  // 查询特定文章
  ARTICLE_URL = '/article/admin/get/',
  // 修改文章
  UPDATE_ARTICLE_URL = '/article/admin/update/',
  // 删除文章
  DELETE_ARTICLE_URL = '/article/admin/delete/',
  // 模糊查询文章
  SEARCH_ARTICLE_URL = '/article/admin/search/',
}

export const reqHasArticle = () =>
  request.get<any, ArticleResponseData>(API.ARTICLE_GETALL_URL)

export const reqArticle = (id: number | string) =>
  request.get<any, ArticleResponseData>(API.ARTICLE_URL + id + '/')

export const reqAddArticle = (data: Article) => {
  return request.post<any, any>(API.ADD_ARTICLE_URL, data)
}

export const reqUpdateArticle = (data: Article) => {
  return request.put<any, any>(API.UPDATE_ARTICLE_URL, data)
}

export const reqDeleteArticle = (id: number) =>
  request.delete<any, any>(API.DELETE_ARTICLE_URL + id + '/')

export const reqSearchArticle = (query: string) =>
  request.get<any, ArticleResponseData>(API.SEARCH_ARTICLE_URL + '?q=' + query)
